﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using WoSoft.PACS.Win.MyToolBars;
using WoSoft.PACS.Win.Forms.SubForms;
using WoSoft.PACS.Data.TemplateData;

namespace WoSoft.PACS.Win.Forms
{
    /// <summary>
    /// 提纲词句管理
    /// </summary>
    public partial class FrmOutline : FrmBase
    {
        private OutlineCategoryDataService outlineCategoryDs = new OutlineCategoryDataService();
        private OutlineWordsDataService outlineWordsDs = new OutlineWordsDataService();
        private string _selectCategoryID = string.Empty;
        private string _oldSelectCategoryID = string.Empty;
        private string _selectWordsID = string.Empty;

        public FrmOutline()
        {
            InitializeComponent();
        }

        public FrmOutline(RibbonBar ribar)
            : base(ribar)
        {
            InitializeComponent();
        }

        public override List<MyToolBars.OperationModel> Operations()
        {
            List<OperationModel> functionInfos = new List<OperationModel>();

            //增加提纲
            var addOutlineButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "增加提纲",
                Image = global::WoSoft.PACS.Win.Properties.Resources.folder_add,
                ClickEvent = AddCategoryClick
            };
            functionInfos.Add(addOutlineButton);

            //删除提纲
            var delOutlineButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "删除提纲",
                Image = global::WoSoft.PACS.Win.Properties.Resources.folder_delete,
                ClickEvent = DeleteClientClick
            };
            functionInfos.Add(delOutlineButton);

            //编辑提纲
            var editOutlineButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "编辑提纲",
                Image = global::WoSoft.PACS.Win.Properties.Resources.folder_edit,
                ClickEvent = EditCategoryClick
            };
            functionInfos.Add(editOutlineButton);

            //增加词句
            var addWordsButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "增加词句",
                Image = global::WoSoft.PACS.Win.Properties.Resources.add2,
                ClickEvent = AddWordsClick
            };
            functionInfos.Add(addWordsButton);

            //删除词句
            var delWordsButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "删除词句",
                Image = global::WoSoft.PACS.Win.Properties.Resources.delete2,
                ClickEvent = DeleteWordsClick
            };
            functionInfos.Add(delWordsButton);

            //修改词句
            var editWordsButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "修改词句",
                Image = global::WoSoft.PACS.Win.Properties.Resources.document_edit,
                ClickEvent = EditWordsClick
            };
            functionInfos.Add(editWordsButton);

            //刷新
            var refreshButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "刷新数据",
                Image = global::WoSoft.PACS.Win.Properties.Resources.refresh,
                ClickEvent = FrmOutline_Load
            };
            functionInfos.Add(refreshButton);
            return functionInfos;
        }


        //添加文件分类
        private void AddCategoryClick(object sender, EventArgs e)
        {
            FrmOutlineCategory category = new FrmOutlineCategory(string.Empty);
            var r = category.ShowDialog(this);
            if (r == System.Windows.Forms.DialogResult.OK)
            {
                RefreshData();
            }
        }

        //删除分类
        private void DeleteClientClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_selectCategoryID))//没有选择分类无法删除
            {
                return;
            }
            if (this.advTree1.SelectedNode.HasChildNodes)//节点下还有子节点，不允许直接删除
            {
                MessageBoxEx.Show("该分类包括子分类，不允许删除！", "提示");
                return;
            }

            //确定要删除
            if (MessageBoxEx.Show("您确定要删除分类吗?删除后所有词句将删除。", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                var cnt = outlineCategoryDs.Delete(_selectCategoryID);
                if (cnt > 0)
                {
                    this.RefreshData();
                }
            }
        }

        //编辑分类
        private void EditCategoryClick(object sender, EventArgs e)
        {

            FrmOutlineCategory category = new FrmOutlineCategory(_selectCategoryID);
            var r = category.ShowDialog(this);
            if (r == System.Windows.Forms.DialogResult.OK)
            {
                RefreshData();
            }
        }

        //添加词句
        private void AddWordsClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_selectCategoryID))
            {//没有选择分类无法添加
                return;
            }
            FrmOutlineWords frmWords = new FrmOutlineWords(_selectCategoryID, string.Empty);
            var r = frmWords.ShowDialog(this);
            if (r == System.Windows.Forms.DialogResult.OK)
            {//刷新数据
                RefreshData();
            }
        }

        //编辑词句
        private void EditWordsClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_selectCategoryID))//没有选择分类无法编辑
            {
                return;
            }
            if (this.superGridControl1.GetSelectedRows().Count == 1)
            {
                DevComponents.DotNetBar.SuperGrid.GridRow gridRow = this.superGridControl1.GetSelectedRows()[0] as DevComponents.DotNetBar.SuperGrid.GridRow;
                FrmOutlineWords frmWords = new FrmOutlineWords(_selectCategoryID, gridRow.Cells["colID"].Value.ToString());
                var r = frmWords.ShowDialog(this);
                if (r == System.Windows.Forms.DialogResult.OK)
                {//刷新数据
                    RefreshData();
                }
            }
        }

        //删除词句
        private void DeleteWordsClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_selectCategoryID))//没有选择分类无法删除
            {
                return;
            }
            if (this.superGridControl1.GetSelectedRows().Count == 1)
            {
                if (MessageBoxEx.Show("您确认要删除选择的词句吗?", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }
                OutlineWordsDataService outlineWordsDataService = new OutlineWordsDataService();
                DevComponents.DotNetBar.SuperGrid.GridRow gridRow = this.superGridControl1.GetSelectedRows()[0] as DevComponents.DotNetBar.SuperGrid.GridRow;
                outlineWordsDataService.Delete(gridRow.Cells["colID"].Value.ToString());
                //刷新用户
                RefreshData();
            }
            else
            {
                MessageBoxEx.Show("请选择一个需要删除的词句");
            }
        }

        //刷新数据
        private void RefreshData()
        {
            //记录旧的ID
            _oldSelectCategoryID = _selectCategoryID;

            var dt = outlineCategoryDs.GetAllData();

            this.advTree1.DisplayMembers = "名称";
            this.advTree1.ValueMember = "ID";
            this.advTree1.DataSource = dt;
            this.advTree1.ParentFieldNames = "ID,上级ID";
            this.advTree1.ExpandAll();

            if (_selectCategoryID != _oldSelectCategoryID)
            {
                this.advTree1.SelectedValue = _oldSelectCategoryID;
            }
        }

        //加载数据
        private void FrmOutline_Load(object sender, EventArgs e)
        {
            RefreshData();

        }

        //选提纲变化
        private void advTree1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (this.advTree1.SelectedValue != null)
            {
                _selectCategoryID = this.advTree1.SelectedValue.ToString();

                var dt = outlineWordsDs.GetModesByCID(_selectCategoryID);
                this.superGridControl1.PrimaryGrid.DataSource = dt;
            }
        }
    }
}
